/**
 * 旅游包车分析
 */

var vehicle;
var enterprise;
$(function(){
	NightDrivingAna.init();
});

var NightDrivingAna = {
	param: {},
	init: function(){
		var me = this;
		me.initQueryTime();
		me.bindQuery();
		me.initweek();
		me.initEmptyList();
		me.loadTableTitle();
		me.queryTable();
	},

    initQueryTime:function(){//默认时间
        var todayDate = new Date();
        var todayDateStr=todayDate.getFullYear()+"-"+(todayDate.getMonth()+1)+"-"+todayDate.getDate();  
        	
        var beforeDate = new Date(todayDate-86400000*1); //前一天
	    var yyyy = beforeDate.getFullYear(), mm = (beforeDate.getMonth() + 1).toString(), dd = beforeDate.getDate().toString();
	    if (mm.length == 1) {
	        mm = '0' + mm;
	    }
	    if (dd.length == 1) {
	        dd = '0' + dd;
	    }
	    $("#day").val(yyyy + '-' + mm + '-' + dd);
	    $("#month").val(yyyy + '-' + mm );
      },
      /**
	    * 事件触发查询
	    */
	    bindQuery: function () {
	    	var me = this;
	    	$("#typeSelDiv").on('change',function(){
				var level = $("#selectTypeSel").val();
				me.initAreaList(level);
			});
	    	$("#exportExcel").bind('click', function () {
	        	me.exportComfirm();
	        });
	        $("#confirmExport").click(function () {
	        	me.exportComfirm();
	        })
	        $('#queryBtn').click(function () {
	            var	areaIds = $("#queryArea").combotree("getValues");
	            if(areaIds.length==0){
	            	global.ajaxLoading.pop("至少选择一个区域", false, 1000);
	                return;
	            }
	            me.queryTable('queryBtn');
	        });
	    },
		  //初始化周维度
	    initweek : function(){
	    	$.ajax({
	    		type : 'post',
	    		url : appCtx + "base/report/PlatformAnalyzes/queryweek.html",
	    		cache : false,
	    		dataType : "JSON",
	    		success : function(data) {
	    			if(data!=null&&data!=''){
	    				$.each(data,function(entryIndex,entry) {
	    					$("#week").append("<option value='"+entry.value+"'>"+entry.text+"</option>");
	    				});				
	    			}
	    		}
	    	});
	    },
	   
		initAreaList:function(level){
			$('#queryArea').combotree({
				url:appCtx + "system/common/queryAreaName.html?level="+level,
				valueField: 'id',
	            textField: 'text',
	            method: 'post',
	            multiple:true,
	            editable : false,
	            collapseAll : true,
	            onSuccess : function(data){
	            },            
	            loadFilter: function(data){
	            	 //data.unshift({ id: '', text: '请选择所属区域', selected: "selected" });
	                 return data;
	    		},
	    		onLoadSuccess: function (data) {
	    			$('#queryArea').combotree('tree').tree("collapseAll");
	    			$('#queryArea').combotree('setText','请选择所属区域')
	         	}
				
			});
		},
		
		initEmptyList: function(){
			var me=this;
	    	$.ajax({
	    		type : 'post',
	    		async : true,
	    		url : appCtx + "system/common/queryLevel.html",
	    		cache : false,
	    		dataType : "JSON",
	    		success : function(data) {			
	    			if(data){
	    				$('#typeSelDiv').html("<select class='query-select' id='selectTypeSel'></select>");
	    				var selectStr="";
	    				for(var i=0;i<data.length;i++){
	    					selectStr+="<option value='"+data[i].id+"'>"+data[i].text+"</option>";
	    				}
	    				$('#selectTypeSel').html(selectStr);
	    				/*$("#selectTypeSel").select2({
	    					minimumResultsForSearch: -1
	    				});*/
	    			}
	    			me.initAreaList(data[0].id);
	    		}
	    	});
	    },
    getColumn:function(){
    	return [{
            display : '排名',
            name : 'ranking',
            width : 5,
            sortable : false,
            align : 'left'
        },{
            display : '得分',
            name : 'score',
            width : 8,
            sortable : false,
            align : 'left',
            showTitle : 'score'
        },{
            display : '区域名称',
            name : 'area_name',
            width : 15,
            sortable : false,
            align : 'left',
            showTitle : 'area_name'
        },{
            display : '车辆入网率(%)',
            name : 'vehicle_access_rate',
            width : 12,
            sortable : false,
            align : 'left',
            paramcolnames:['vehicle_access_rate'],
            handlefunction:function(paramcolnames){
            	var handleStr =Math.round(paramcolnames*Math.pow(10,2))/Math.pow(10,2);	
        		return handleStr;
            }
        },{
            display : '车辆上线率(%)',
            name : 'vehicle_online_rate',
            width : 12,
            sortable : false,
            align : 'left',
            paramcolnames:['vehicle_online_rate'],
            handlefunction:function(paramcolnames){
            	var handleStr =Math.round(paramcolnames*Math.pow(10,2))/Math.pow(10,2);	
        		return handleStr;
            }
        },{
            display : '在岗率(%)',
            name : 'reply_rate',
            width : 12,
            sortable : false,
            align : 'left',
            paramcolnames:['reply_rate'],
            handlefunction:function(paramcolnames){
            	var handleStr =Math.round(paramcolnames*Math.pow(10,2))/Math.pow(10,2);	
        		return handleStr;
            }
        },{
            display : '轨迹完整率(%)',
            name : 'track_complete_rate',
            width : 12,
            sortable : false,
            align : 'left',
            paramcolnames:['track_complete_rate'],
            handlefunction:function(paramcolnames){
            	var handleStr =Math.round(paramcolnames*Math.pow(10,2))/Math.pow(10,2);	
        		return handleStr;
            }
        },{
            display : '数据合格率(%)',
            name : 'data_qualified_rate',
            width : 12,
            sortable : false,
            align : 'left',
            paramcolnames:['exception_track_count','track_normal'],
            handlefunction:function(exception_track_count,track_normal){
            	var handleStr;
            	var count = (Number(track_normal)+Number(exception_track_count));
            	var param = 1-Number(exception_track_count)/count;
            	if(count==0){
            		handleStr="0"
            	}else{
            		handleStr = handleStr =Math.round((param*100)*Math.pow(10,2))/Math.pow(10,2);	
            	}
        		return handleStr;
            }
        /*},{
            display : '安全报警',
            name : 'adas_alarm_count',
            width : 12,
            sortable : false,
            align : 'left'*/
           
        }]
    },

    loadTableTitle:function(){
     	 
     	 var height = window.parent.nav.whiceH[1]-177;
    	$("#objTable").flexigrid({
	            dataType : 'json',
	            colModel :this.getColumn() ,
	            usepager : false,// 是否分页，默认为true。
	            height:height,
	            widthType:'%',
	            singleSelect:true,
	            width: window.parent.$("iframe").width(),
	            rp : 100000,
	            onSelectRow:function(row){
	            	//window.open(global.url.domain+'gps-web/base/report/AreaAnalyzes/pageToAreaAnalyzes.html?pageAreaId='+row.area_id+'&pageVeidoo='+veidoo+'&pageAnalyse_date='+analyse_date);
	            	switchoverfow(row.area_id,row.area_name);
	            }
	         });
    },
    exportExcel: function(){
    	var total = $("#objTable").getOptions().total;
		if (total == 0) {
	          global.ajaxLoading.pop("无导出数据", false, 2000);
	          return;
	    }
    	$("#exportModal").modal("show");
	},		
    /**
     * 导出
     */
	exportComfirm: function () {
    	var me = this;
    	var	areaIds = $("#queryArea").combotree("getValues");
    	var veidoo = $("#veidoo").val();
    	var analyse_date = null;
    	if(veidoo=='day'){
    		analyse_date = $("#day").val();
    	}else if(veidoo=='week'){
    		analyse_date = $("#week").val();
    	}else if(veidoo=='month'){
    		analyse_date = $("#month").val();
    	}
        var options = $("#objTable").getOptions();
        if (total == 0) {
            global.ajaxLoading.pop("无导出数据", false, 2000);
            return;
        }
        //总记录数
        var total = options.total;
        window.parent.global.ajaxLoading.pop("正在导出,请稍后.....", true, 2000);
        var adasJson={
        		"display":"安全报警",
        		"name":"adas_alarm_count",
        		"width":120,
        		"sortable":false,
        		"align":"left"
        };
        //var adasJson = JSON.parse(adasJsonStr);
        
        var columns = NightDrivingAna.getColumn2();
        //columns.push(adasJson);
        var header1 = JSON.stringify(columns);
     
        var colModelList = new Array();//表头
        colModelList.push(header1);
        var colModel = $.toJSON(colModelList);
        var paramsGrid = {
           colModel: colModel,
            title: "区域分析考核表",
            imgsrc: null,
            veidoo:veidoo,
			analyse_date:analyse_date,
			areaIds:areaIds,
			level:$("#selectTypeSel").val()
        };
        post(appCtx + "base/report/AreaAnalyzes/exportAreaAssessment.html", paramsGrid);
    },
    /**
     * 获取表格字段头
     */
    getColumn2: function() {
    	return [{
            display : '排名',
            name : 'ranking',
            width : 120,
            sortable : false,
            align : 'left'
        },{
            display : '得分',
            name : 'score',
            width : 120,
            sortable : false,
            align : 'left',
        },{
            display : '区域名称',
            name : 'area_name',
            width : 150,
            sortable : false,
            align : 'left',
        },{
            display : '车辆入网率',
            name : 'vehicle_access_rate',
            width : 120,
            sortable : false,
            align : 'left'
        },{
            display : '车辆上线率',
            name : 'vehicle_online_rate',
            width : 120,
            sortable : false,
            align : 'left'
        },{
            display : '在岗率',
            name : 'reply_rate',
            width : 120,
            sortable : false,
            align : 'left'
        },{
            display : '轨迹完整率',
            name : 'track_complete_rate',
            width : 120,
            sortable : false,
            align : 'left'
        },{
            display : '数据合格率',
            name : 'data_qualified_rate',
            width : 120,
            sortable : false,
            align : 'left'
        }];
    },
    
    queryTable:function(name){
    	var me = this;
    	var veidoo = $("#veidoo").val();
    	var level = $("#selectTypeSel").val();
    	if(veidoo=='day'){
    		analyse_date = $("#day").val();
    	}else if(veidoo=='week'){
    		analyse_date = $("#week").val();
    	}else if(veidoo=='month'){
    		analyse_date = $("#month").val();
    	}
    	if(name=='queryBtn'){
    		var	areaIds = $("#queryArea").combotree("getValues");
    	}
    	$.ajax({
    		url:appCtx+"base/report/AreaAnalyzes/queryAreaAnalyzes.html",
    		type:"post",
			async : true,
			cache : false,
    		data:{
    			veidoo:veidoo,
    			analyse_date:analyse_date,
    			level:level,
    			areaIds:areaIds
    		},
    		dataType:"json",
    		success:function(data){
        			if(data.listAnalyzes.length==0){
        				var dataRows= {
        			     		rows:data.listAnalyzes,
        			     		total:data.listAnalyzes.length,
        			     		page:1
        			     	};
        				$("#objTable").flexAddData(dataRows);
        			}else{
        				var dataRows= {
        			     		rows:data.listAnalyzes,
        			     		total:data.listAnalyzes.length,
        			     		page:1
        			     	};
        		     	$("#objTable").flexAddData(dataRows);
    			}
    		}
    	});
    }
};
function selectveidoo(){
	var vo = $("#veidoo").val();
	if(vo=='day'){
		$("#day").show();
		$("#week").hide();
		$("#month").hide();
	}else if(vo=='week'){
		$("#day").hide();
		$("#week").show();
		$("#month").hide();
	}else if(vo=='month'){
		$("#day").hide();
		$("#week").hide();
		$("#month").show();
	}
}

function switchoverfow(area_id,area_name){
	window.parent.$("#menuContAL").show();
	window.parent.$("#menuContQC").hide();
	window.parent.$("#AL").attr("class","fl active");
	window.parent.$("#QC").attr("class","fl");
	
	var veidoo = $('#veidoo').val();
	var level = $('#selectTypeSel').val();
	var analyse_date = null;
	if(veidoo=='day'){
		analyse_date = $("#day").val();
	}else if(veidoo=='week'){
		analyse_date = $("#week").val();
	}else if(veidoo=='month'){
		analyse_date = $("#month").val();
	}
	window.parent.frames[0].callback(veidoo,area_id,level,analyse_date,area_name);
}
